package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import model.Accident;
import model.Accisqe;
import model.AciActivity;
import model.AciCause;
import model.Injured;
import model.ProduceType;
import model.Producer;
import model.Product;
import model.User;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

import action.base.BaseAction;
import service.AccidentService;
import service.AccisqeService;
import service.AciActiService;
import service.AciCauseService;
import service.InjuredService;
import service.ProduceTypeService;
import service.ProducerService;
import service.ProductServive;
import util.SaveFile;

/**
 * 报告单管理
 * 
 * @author yakun0622
 * 
 */
@Controller
@Scope("prototype")
public class AccidentAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private AccidentService accidentService;
	private Accident accident;
	@Autowired
	private AccisqeService accisqeService;
	private Accisqe accisqe;
	@Autowired
	private AciActiService aciActiService;
	private AciActivity aciActivity;
	@Autowired
	private AciCauseService aciCauseService;
	private AciCause aciCause;
	@Autowired
	private ProductServive productServive;
	private Product product;
	@Autowired
	private ProducerService producerService;
	private Producer producer;
	@Autowired
	private ProduceTypeService produceTypeService;
	private ProduceType produceType;
	@Autowired
	private InjuredService injuredService;
	private Injured injured;
	private boolean success = false;
	private PageList<HashMap<String, Object>> pageList;
	private List<HashMap<String, Object>> acciInfo;
	private List<HashMap<String, Object>> acciActis;
	private List<HashMap<String, Object>> acciCauses;
	private List<HashMap<String, Object>> prodTypes;
	private int total;
	private File file;
	// 获取上传文件的名称
	private String fileFileName;
	private InputStream stream;
	private Integer acciId;
	private Integer countMy;

	public String saveOrUpdate() {
		String prodtypes = super.getParameterStr("prodtypes_value");
		String causes = super.getParameterStr("causes_value");
		String activities = super.getParameterStr("activities_value");
		User user = super.getUser();
		accidentService.saveOrUpdate(user, accident, accisqe, product,
				producer, injured, prodtypes, causes, activities);
		String filePath = saveFile();
		accident.setAttach(filePath);
		accident.setAttachname(fileFileName);
		accidentService.saveOrUpdate(user, accident, accisqe, product,
				producer, injured, prodtypes, causes, activities);
		success = true;
		return "save";
	}

	public String saveFile() {
		Integer _fname;
		if (acciId == null) {
			_fname = accident.getId();
		} else {
			_fname = acciId;
		}

		SaveFile sf = new SaveFile();
		String path = super.getContext().getRealPath(
				"\\uploadFiles\\acciFiles\\f_");

		String filePath = null;
		String fname = _fname.toString();
		if (file != null && file.exists() && file.length() > 0) {
			filePath = sf.save2Server(fname, file, path);
		}
		return filePath;
	}

	public String down() throws Exception {
		Integer id = super.getParameterInt("acciId");
		String fname = super.getParameterStr("fileName");
		acciInfo = accidentService.getById(id);
		if (StringUtils.isNotEmpty((String) acciInfo.get(0).get("attach"))) {
			String attach = (String) acciInfo.get(0).get("attach");
			File f = new File(attach);
			if (f.exists()) {
				setStream(new FileInputStream(f));

			} else
				return "noFile";
		} else
			return "noFile";
		success = true;
		fileFileName = fname;
		return "down";

	}

	public String countMy() {
		Integer status = super.getParameterInt("status");
		User user = super.getUser();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("status", status);
		map.put("userid", user.getId());
		countMy = accidentService.countMy(map);
		setSuccess(true);
		return "countMy";
	}

	public String queryPage() throws UnsupportedEncodingException {
		int page = super.getParameterInt("page");
		int limit = super.getParameterInt("limit");
		Integer status = super.getParameterInt("status");
		Integer select = super.getParameterInt("select");
		String prodName = super.getParameterStr("prodName");
		String beginDate = super.getParameterStr("beginDate");
		// String prodtype = super.getParameterStr("prodtype");
		Integer prodtype = super.getParameterInt("prodtype");
		String endDate = super.getParameterStr("endDate");
		String flag = super.getParameterStr("flag");
		User user = super.getUser();
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isNotBlank(prodName)) {
			prodName = null;
		} else {
			byte[] _prodName = prodName.getBytes("ISO-8859-1");
			prodName = new String(_prodName, "UTF-8");
		}
		if (!StringUtils.isNotBlank(beginDate)) {
			beginDate = null;
		}
		// if (!StringUtils.isNotBlank(prodtype)) {
		// prodtype = null;
		// } else {
		// byte[] _prodtype = prodtype.getBytes("ISO-8859-1");
		// prodtype = new String(_prodtype, "UTF-8");
		// if (prodtype.equals("全部")) {
		// prodtype = null;
		// }
		// }
		if (!StringUtils.isNotBlank(endDate)) {
			endDate = null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (select != null && select == 1) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			beginDate = sdf.format(new Date());
			calendar.add(Calendar.MONTH, -1);
			beginDate = sdf.format(calendar.getTime());
			endDate = sdf.format(new Date());
		} else if (select != null && select == 2) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			beginDate = sdf.format(new Date());
			calendar.add(Calendar.MONTH, -3);
			beginDate = sdf.format(calendar.getTime());
			endDate = sdf.format(new Date());
		}
		if (status != null) {
			if (status == 0) {
				status = null;
			}
		}
		if (select != null) {
			if (select == 0) {
				select = null;
			}
		}
		if (prodtype != null) {
			if (prodtype == 0) {
				prodtype = null;
			}
		}
		map.put("prodName", prodName);
		map.put("beginDate", beginDate);
		map.put("prodtype", prodtype);
		map.put("endDate", endDate);
		map.put("status", status);
		map.put("flag", flag);
		pageList = accidentService.queryPage(page, limit, user, map);
		setTotal(pageList.getPaginator().getTotalCount());
		return "queryPage";
	}

	public String getById() {
		Integer id = super.getParameterInt("accidentId");
		acciInfo = accidentService.getById(id);
		success = true;
		return "acciInfo";
	}

	public String getActi() {
		Integer id = super.getParameterInt("accidentId");
		acciActis = accidentService.getActi(id);
		success = true;
		return "acciActis";
	}

	public String getCause() {
		Integer id = super.getParameterInt("accidentId");
		acciCauses = accidentService.getCause(id);
		success = true;
		return "acciCauses";
	}

	public String getType() {
		Integer id = super.getParameterInt("accidentId");
		prodTypes = accidentService.getType(id);
		success = true;
		return "prodTypes";
	}

	public String delete() {
		String ids = super.getParameterStr("ids");
		accidentService.delete(ids);
		success = true;
		return "success";
	}

	public String submit() {
		String ids = super.getParameterStr("ids");
		accidentService.submit(ids);
		success = true;
		return "success";
	}

	public Accident getAccident() {
		return accident;
	}

	public void setAccident(Accident accident) {
		this.accident = accident;
	}

	public Accisqe getAccisqe() {
		return accisqe;
	}

	public void setAccisqe(Accisqe accisqe) {
		this.accisqe = accisqe;
	}

	public AciActivity getAciActivity() {
		return aciActivity;
	}

	public void setAciActivity(AciActivity aciActivity) {
		this.aciActivity = aciActivity;
	}

	public AciCause getAciCause() {
		return aciCause;
	}

	public void setAciCause(AciCause aciCause) {
		this.aciCause = aciCause;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Producer getProducer() {
		return producer;
	}

	public void setProducer(Producer producer) {
		this.producer = producer;
	}

	public ProduceType getProduceType() {
		return produceType;
	}

	public void setProduceType(ProduceType produceType) {
		this.produceType = produceType;
	}

	public Injured getInjured() {
		return injured;
	}

	public void setInjured(Injured injured) {
		this.injured = injured;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public PageList<HashMap<String, Object>> getPageList() {
		return pageList;
	}

	public void setPageList(PageList<HashMap<String, Object>> pageList) {
		this.pageList = pageList;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List<HashMap<String, Object>> getAcciInfo() {
		return acciInfo;
	}

	public void setAcciInfo(List<HashMap<String, Object>> acciInfo) {
		this.acciInfo = acciInfo;
	}

	public List<HashMap<String, Object>> getAcciActis() {
		return acciActis;
	}

	public void setAcciActis(List<HashMap<String, Object>> acciActis) {
		this.acciActis = acciActis;
	}

	public List<HashMap<String, Object>> getAcciCauses() {
		return acciCauses;
	}

	public void setAcciCauses(List<HashMap<String, Object>> acciCauses) {
		this.acciCauses = acciCauses;
	}

	public List<HashMap<String, Object>> getProdTypes() {
		return prodTypes;
	}

	public void setProdTypes(List<HashMap<String, Object>> prodTypes) {
		this.prodTypes = prodTypes;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public InputStream getStream() {
		return stream;
	}

	public void setStream(InputStream stream) {
		this.stream = stream;
	}

	public Integer getAcciId() {
		return acciId;
	}

	public void setAcciId(Integer acciId) {
		this.acciId = acciId;
	}

	public Integer getCountMy() {
		return countMy;
	}

	public void setCountMy(Integer countMy) {
		this.countMy = countMy;
	}

}
